package com.hzit.servlet.ajax;

import com.alibaba.fastjson.JSONArray;
import com.hzit.bean.Dept;
import com.hzit.dao.DeptDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "FindDeptListServletAjax",urlPatterns = "/dept/ajax/findAll")
public class FindDeptListServletAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //数据库获取数据
        response.setContentType("application/json;charset=utf-8");


        DeptDao deptDao = new DeptDao();
        List<Dept> deptList = deptDao.findAll();

        //转为json格式的数据
        String json = JSONArray.toJSONString(deptList);

        response.getWriter().print(json);


    }
}
